/**
 * 
 */
package com.goodedeeds.webapp.controller;

import com.goodedeeds.webapp.LoginHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.validation.EmailTypeConverter;
import net.sourceforge.stripes.validation.SimpleError;
import net.sourceforge.stripes.validation.Validate;
import net.sourceforge.stripes.validation.ValidationErrors;

/**
 * @author ravi
 *
 */
@UrlBinding(RegisterAction.URL)
public class RegisterAction extends QuiggleAction{
	
	public static final String URL = "/register.action";
	
	
	@Validate(required=true, on={"register", "sendPassword"}, converter=EmailTypeConverter.class)
	private String email;

	@Validate(required=true, on="register")
	private String password;

    @Validate(required=true, on="register")
    private String referral;

    @Validate(required=true, on="register")
	private String password2;

	@Validate(required=true, on="register")
	private String firstName;
	
	@Validate(required=true, on="register")
	private String lastName;

    public static final String VIEW = JSP_ROOT + "/login.jsp";
    public static final String REGISTER = JSP_ROOT + "/register.jsp";
	/* (non-Javadoc)
	 * @see com.depo.quiggle.view.controller.BaseAction#getBodyContent()
	 */
	@Override
	public String getQuiggleHeaderInclude() {
		return VIEW;
	}

    @Override
    public String getQuiggleContent() {
        return REGISTER;
    }

	
	public Resolution register(){

        if(!password.equals(password2)){
           SimpleError error =  new SimpleError("Password and retype password does not match.");
           addValidationError("password", error);
           addValidationError("password2", error);
           return showPage();
        }

//		IAdministrationDao dao = AdministrationDaoFactory.getAdministrationDao();
		
//		IUser user = dao.createNewUser(1);

//		user.setEmail(getEmail());
//		user.setFirstName(getFirstName());
//		user.setLastName(getLastName());
//		user.setPassword(getPassword());
//		user.setReferral(getReferral());

		try{
//		    dao.saveUser(user);
//		    dao.createMasterRateForAUser(user.getUserId());
//            LoginHandler.getInstance().sendLockedEmail(user.getEmail(), user.getUnlockCode());
        }catch (Exception e){
            ValidationErrors errors = new ValidationErrors();
            errors.add("email", new SimpleError("Email already exists."));
            getContext().setValidationErrors(errors);

            return showPage();
        }
		
		LoginHandler.getInstance().doLogin(getEmail(),getPassword(), getContext().getRequest(), getContext().getResponse());
		
		return new RedirectResolution("");
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the password2
	 */
	public String getPassword2() {
		return password2;
	}

	/**
	 * @param password2 the password2 to set
	 */
	public void setPassword2(String password2) {
		this.password2 = password2;
	}

	/**
	 * @return the firstName
	 */
	public String getFirstName() {
		return firstName;
	}

	/**
	 * @param firstName the firstName to set
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return the lastName
	 */
	public String getLastName() {
		return lastName;
	}

	/**
	 * @param lastName the lastName to set
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	
    public String getReferral() {
        return referral;
    }

    public void setReferral(String referral) {
        this.referral = referral;
    }

    public String getLoginEmail(){
        return null;
    }
}
